<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-form :inline="true" :model="queryForm" class="demo-form-inline" ref="queryForm">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="queryForm.name"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="" @click="reset" :disabled="isLoading">重置</el-button>
            <el-button type="info" @click="search" :disabled="isLoading">查询搜索</el-button>
          </el-form-item>
        </el-form>
        <div slot="header" class="clearfix">
        <div>
          <el-button size="mini" @click="handleAdd">添加角色</el-button>
        </div>
      </div>
      </div>
      <el-table :data="roleList" style="width: 100%" border v-loading="isLoading">
        <el-table-column label="编号" type="index" width="50">
        </el-table-column>
        <el-table-column prop="name" label="角色名称">
        </el-table-column>
        <el-table-column prop="description" label="描述">
        </el-table-column>
        <el-table-column prop="createdTime" label="添加时间" :formatter="formatDate">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="$router.push({name: 'alloc-menu', params: { roleId: scope.row.id }})">分配菜单</el-button>
            <el-button size="mini" type="text" @click="handleDelete(scope.row)">分配资源</el-button>
            <el-button size="mini" type="text" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="text" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="queryForm.current"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="queryForm.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      style="padding: 10px;"
      :disabled="isLoading">
      </el-pagination>
    </el-card>
    <el-dialog :title="isEdit ? '编辑角色': '添加角色'" :visible.sync="dialogVisible" width="30%">
      <create-or-edit @success="onSuccess" @cancel="dialogVisible = false" :role-id="roleId" :isEdit="isEdit" v-if="dialogVisible"></create-or-edit>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { getRoleList, deleteRoleById } from '@/services/role'
import { Form } from 'element-ui'
import { formatDate } from '@/utils/formatDate'
import CreateOrEdit from './CreateOrEdit.vue'
export default Vue.extend({
  name: 'RoleList',
  components: {
    CreateOrEdit
  },
  data () {
    return {
      queryForm: {
        name: '',
        current: 1,
        size: 10
      },
      roleList: [],
      isLoading: true,
      total: 0,
      dialogVisible: false,
      roleId: null,
      isEdit: false
    }
  },
  created () {
    this.loadRoleList()
  },
  methods: {
    async loadRoleList () {
      this.isLoading = true
      const { data } = await getRoleList(this.queryForm)
      this.roleList = data.data.records
      this.total = data.data.total
      this.isLoading = false
    },
    handleAdd () {
      this.dialogVisible = true
    },
    reset () {
      (this.$refs.queryForm as Form).resetFields()
      this.queryForm.current = 1
      this.loadRoleList()
    },
    search () {
      this.loadRoleList()
    },
    formatDate,
    onSuccess () {
      this.dialogVisible = false
      this.loadRoleList()
    },
    handleSizeChange (val: any) {
      this.queryForm.size = val
      this.queryForm.current = 1
      this.loadRoleList()
    },
    handleCurrentChange (val: any) {
      this.queryForm.current = val
      this.loadRoleList()
    },
    handleEdit (item: any) {
      this.dialogVisible = true
      this.roleId = item.id
      this.isEdit = true
    },
    async handleDelete (item: any) {
      this.$confirm('是否确定删除', '删除提示', {}).then(async () => {
        await deleteRoleById(item.id)
        this.loadRoleList()
      }).catch(() => {
        this.$message.info('已取消删除操作')
      })
    }
  }
})
</script>

<style lang="scss" scope></style>
